﻿using Microsoft.AspNetCore.Http;
using System.Text;
using System.Threading.Tasks;

namespace NetCore.Middleware
{
    /// <summary>
    /// app.UseMiddleware<BowenMiddleware>();
    /// </summary>
    public class BowenMiddleware
    {
        private readonly RequestDelegate _next;//执行下一步的请求委托

        public BowenMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext context)
        {
            //中文输出不乱码  需要配置context的头
            if (context.Request.Path.Value.Contains("Bowen"))
            {
                await context.Response.WriteAsync($"{nameof(BowenMiddleware)}输入的字符串包含Bowen此分支不会继续执行{nameof(RequestDelegate)}" +
                 $"所以这里是Bowen的终结点<br/>", Encoding.UTF8);
            }
            else
            {
                await context.Response.WriteAsync($"{nameof(BowenMiddleware)} continue1");
                await _next(context);//俄罗斯套娃，继续先执行后面的逻辑
                await context.Response.WriteAsync($"{nameof(BowenMiddleware)} continue2");
            }
        }
    }
}
